package com.xiaoshuidi.cloud.module.contract.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.bpm.enums.BpmBusinessTypeEnum;
import com.xiaoshuidi.cloud.module.bpm.mq.message.BpmProcessInstanceResultEvent;
import com.xiaoshuidi.cloud.module.contract.api.ContractApi;
import com.xiaoshuidi.cloud.module.contract.enums.ApproveTypeEnum;
import com.xiaoshuidi.cloud.module.contract.service.bill.OwnerOrderService;
import com.xiaoshuidi.cloud.module.contract.service.bill.ReadjustRecordService;
import com.xiaoshuidi.cloud.module.contract.service.bill.RentOrderService;
import com.xiaoshuidi.cloud.module.contract.service.contract.CompanyContractService;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractHouseOwnerService;
import com.xiaoshuidi.cloud.module.contract.vo.ApproveReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class BpmProcessInstanceResultConsumer implements ApplicationListener<BpmProcessInstanceResultEvent> {
    @Resource
    private ContractApi contractApi;
    @Resource
    private RentOrderService rentOrderService;
    @Resource
    private CompanyContractService companyContractService;
    @Resource
    private ContractHouseOwnerService contractHouseOwnerService;

    @Resource
    private OwnerOrderService ownerOrderService;
    @Resource
    private ReadjustRecordService readjustRecordService;


    @Override
    public void onApplicationEvent(BpmProcessInstanceResultEvent event) {
        log.info("收到工作流状态变更消息{}", JsonUtils.toJsonString(event));
        try {
            ApproveReqVO approveReqVO = new ApproveReqVO();
            approveReqVO.setId(Long.parseLong(event.getBusinessKey()));
            approveReqVO.setProcessId(event.getId());
            approveReqVO.setResult(event.getResult());
            if (BpmBusinessTypeEnum.CONTRACT.name().equals(event.getBusinessType())) {
                log.info("合同审批====:{}", JSONObject.toJSONString(approveReqVO));
                if (ApproveTypeEnum.RENTING_OUT_CONTRACT.getMsg().equals(event.getProcessDefinitionCategory())) {
                    //退租
                    log.info("退租合同审批====:{}", JSONObject.toJSONString(approveReqVO));
                    contractApi.updateRentingOutContractStatus(approveReqVO);
                } else {
                    log.info("新签续租换房合同审批====:{}", JSONObject.toJSONString(approveReqVO));
                    contractApi.updateContractStatus(approveReqVO);
                }
            } else if (BpmBusinessTypeEnum.RENT_ORDER.name().equals(event.getBusinessType())) {
                log.info("账单审批====:{}", JSONObject.toJSONString(approveReqVO));
                rentOrderService.orderApproval(approveReqVO);
            } else if (BpmBusinessTypeEnum.COMPANY_CONTRACT.name().equals(event.getBusinessType())) {
                log.info("企业合同新签审批====:{}", JSONObject.toJSONString(approveReqVO));
                log.info("创建企业合同,企业合同id:{}", event.getBusinessKey());
                log.info("创建企业合同,流程id:{}", event.getId());
                log.info("创建企业合同,合同审批状态：{}", event.getResult());

                log.info("审批类型：{}",event.getProcessDefinitionCategory());
                if (ApproveTypeEnum.RENTING_OUT_COMPANY_CONTRACT.getMsg().equals(event.getProcessDefinitionCategory())){
                    log.info("企业子合同审批====:{}", JSONObject.toJSONString(approveReqVO));
                    companyContractService.updateContractStatus(approveReqVO);
                }else if(ApproveTypeEnum.RENEWAL_COMPANY_CONTRACT.getMsg().equals(event.getProcessDefinitionCategory())){
                    companyContractService.batchRenewalCompanyContract(approveReqVO);
                }
                else {
                    companyContractService.createSubContractAndStatus(approveReqVO);
                }
            } else if (BpmBusinessTypeEnum.OWNER_CONTRACT.name().equals(event.getBusinessType())) {
                log.info("新增业主合同审批====:{}", JSONObject.toJSONString(approveReqVO));
                contractHouseOwnerService.updateContractStatus(approveReqVO);
            } else if (BpmBusinessTypeEnum.PAYMENT_BY_THE_OWNER.name().equals(event.getBusinessType())) {
                log.info("业主账单打款审批====:{}", JSONObject.toJSONString(approveReqVO));
                ownerOrderService.approvalResult(event);
            } else if (BpmBusinessTypeEnum.COMPANY_CONTRACT_BATCH_CHECKOUT.name().equals(event.getBusinessType())) {
                log.info("企业批量退租审批====:{}", JSONObject.toJSONString(approveReqVO));
                companyContractService.approvalResult(event);
            } else if (BpmBusinessTypeEnum.HOUSE_OWNER_RENTING_OUT.name().equals(event.getBusinessType())) {
                log.info("业主退租审批====:{}", JSONObject.toJSONString(approveReqVO));
                contractHouseOwnerService.approvalResult(event);
            } else if (BpmBusinessTypeEnum.HOUSE_OWNER_RENEWAL.name().equals(event.getBusinessType())) {
                log.info("业主合同续租审批====:{}", JSONObject.toJSONString(approveReqVO));
                contractHouseOwnerService.updateContractStatus(approveReqVO);
            } else  if(BpmBusinessTypeEnum.READJUST_ORDER_FEE.name().equals(event.getBusinessType())){
                log.info("冲调审批审批====:{}", JSONObject.toJSONString(approveReqVO));
                readjustRecordService.callBackReadjust(event);
            }

        } catch (ServiceException ignore) {
            //业务异常不往外抛, 不然消息会重复消费
        }
    }
}
